package com.demo.service.impl;


import java.util.Date;
import java.util.List;

import com.demo.entity.po.ChatMessage;

import com.demo.entity.po.UserInfo;
import com.demo.entity.query.ChatMessageQuery;

import com.demo.entity.query.UserInfoQuery;
import com.demo.entity.vo.PaginationResultVO;

import com.demo.mappers.UserInfoMapper;
import com.demo.service.ChatMessageService;

import com.demo.service.UserInfoService;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.ChatMessageMapper;

import com.demo.entity.enums.PageSize;

/**
 * @Description:聊天消息表serviceImpl
 * @Date:2025-03-09
 * @author：author
*/
@Service("chatMessageService")
public class ChatMessageServiceImpl implements ChatMessageService{

	@Resource
	private ChatMessageMapper<ChatMessage,ChatMessageQuery> chatMessageMapper;

	@Qualifier("requestMappingHandlerMapping")
    @Autowired
    private BeanNameAware beanNameAware;

	@Resource
	private UserInfoMapper<UserInfo, UserInfoQuery> userInfoMapper;


	/**
	 * 根据条件查询列表
	 */
	public List<ChatMessage> findListByParam(ChatMessageQuery query) { 
		return this.chatMessageMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(ChatMessageQuery query){
		return this.chatMessageMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<ChatMessage> findListByPage(ChatMessageQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<ChatMessage> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(ChatMessage bean) { 
		return this.chatMessageMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<ChatMessage> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.chatMessageMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<ChatMessage> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.chatMessageMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据MessageId查询
	 */
	public ChatMessage getChatMessageByMessageId(Long messageId) { 
		return this.chatMessageMapper.selectByMessageId(messageId);
	}

	/**
	 * 根据MessageId更新
	 */
	public Integer updateChatMessageByMessageId(ChatMessage bean, Long messageId) { 
		return this.chatMessageMapper.updateByMessageId(bean,messageId);
	}

	/**
	 * 根据MessageId删除
	 */
	public Integer deleteChatMessageByMessageId(Long messageId) { 
		return this.chatMessageMapper.deleteByMessageId(messageId);
	}

	@Override
	public void sendMessage(ChatMessage bean) {
		UserInfo userInfoDB = userInfoMapper.selectByUserId(Long.parseLong(bean.getSendUserId()));
		bean.setSendTime(new Date().getTime());
		bean.setSendUserNickName(userInfoDB.getUsername());
		chatMessageMapper.insert(bean);
	}
}